package com.softaria.windows.logic;

import java.util.HashSet;

import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.softaria.windows.widgets.Frame;
import com.softaria.windows.widgets.TabbedWorkPanel;

public class MaximizedFramesResizeManager implements ResizeHandler {

  private HashSet<Frame> maximizedFrames = new HashSet<Frame>();
  private TabbedWorkPanel.Tab activeTab;

  public void addFrame(Frame frame) {
    maximizedFrames.add(frame);
  }

  public void removeFrame(Frame frame) {
    maximizedFrames.remove(frame);
  }

  public void setActivePanel(TabbedWorkPanel.Tab panel) {
    this.activeTab = panel;
    updateFrameSize();
  }

  @Override
  public void onResize(ResizeEvent event) {
    if (activeTab != null) {
      updateFrameSize();
    }
  }

  private void updateFrameSize() {

    activeTab.updateSize();

    for (Frame frame : maximizedFrames) {
      if (frame.getContainer().equals(activeTab.getPanel())) {
        frame.updateMaximizedSize();
      }
    }

  }

}
